import java.awt.*;
public class Line {
    //起点坐标
    int x = 380;
    int y = 180;
    //终点坐标
    int endX = 500;
    int endY = 500;
    //线长
    double length = 100;
    //线长最小值
    double MIN_length = 100;
    //线长最大值
    double MAX_length = 750;
    double n = 0;
    //方向
    int dir = 1;
    //状态：0摇摆 1抓取 2收回 3抓取返回
    int state = 0;
    //钩爪图片
    Image hook = Toolkit.getDefaultToolkit().getImage("imgs/hook.png");

    GameWin frame;
    Line(GameWin frame){

        this.frame = frame;
    }
    /**
     * 描述：碰撞检测,检测物体是否被抓取
     * @param
     * @author 发财
     * @return
     */
    void logic(){
    }
    /**
     * 描述：绘制方法
     * @param
     * @author 黄钰韫
     * @return
     */
    void lines(Graphics g){
        endX = (int) (x+length*Math.cos(n*Math.PI));
        endY = (int) (y+length*Math.sin(n*Math.PI));
        g.setColor(Color.red);
        g.drawLine(x-1,y-1,endX,endY);
        g.drawLine(x,y,endX,endY);
        g.drawLine(x+1,y+1,endX,endY);
        g.drawImage(hook,endX-36,endY-2,null);
    }
    /**
     * 描述：绘制线
     * @param
     * @author 黄钰韫
     * @return
     */
    void paintSelf(Graphics g){
        logic();
        switch (state){
            case 0:
                if(n < 0.1){
                    dir = 1;
                }else if(n > 0.9){
                    dir = -1;
                }
                n += 0.005*dir;
                lines(g);
                break;
            case 1:
                if(length<MAX_length){
                    length=length+5;
                    lines(g);
                }else {state=2;}
                break;
            case 2:
                if(length>MIN_length){
                    length=length-5;
                    lines(g);
                }else {
                    state=0;
                }
                break;
            case 3:
                int m = 1;
                if(length>MIN_length){
                    length=length-5;
                    lines(g);
                    for(Object obj:this.frame.objectList){
                        if(obj.flag){
                            m = obj.m;
                            obj.x=endX-obj.getWidth()/2;
                            obj.y=endY;
                            if(length<=MIN_length){
                                obj.x=-150;
                                obj.y=-150;
                                obj.flag=false;
                                Bg.waterFlag=false;
                                //加分
                                Bg.count+=obj.count;
                                state=0;
                            }
                            if(Bg.waterFlag){
                                if(obj.type == 1){
                                    m = 1;//通过降低质量来提高抓取频率
                                }
                                if(obj.type == 2){//爆破移除
                                    obj.x=-150;
                                    obj.y=-150;
                                    obj.flag=false;
                                    Bg.waterFlag = false;
                                    state = 2;
                                }
                            }
                        }

                    }
                }
                try {
                    Thread.sleep(m);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                break;
            default:
        }
    }

    /**
     * 描述：重置线
     * @param
     * @author  黄钰韫
     * @return
     */
    void reGame(){
        n = 0;//线的角度百分比
        length = 100;
    }
}
